package com.szjlc.responsebodyadvice.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.szjlc.responsebodyadvice.entity.Employee;
import com.szjlc.responsebodyadvice.interception.ResponseAdvice;
import com.szjlc.responsebodyadvice.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Resource;
import javax.validation.constraints.Min;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.env.Environment;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 雇员前端控制层
 * @author steve
 * @since 2022-11-19 14:39
 */
@Slf4j
@Validated
@RestController
@RequestMapping(value = "/employee", produces = "application/json;charset=UTF-8")
@Api(tags = {"雇员前端控制层"})
@ApiSupport(author = "steve-class,email=1518109600@qq.com")
public class EmployeeController extends ResponseAdvice {

    @Resource
    private EmployeeService employeeService;

    @Resource
    private Environment environment;

    /** 查询全部 */
    @ApiOperation("查询全部")
    // @ApiOperationSupport(author="steve,email=1518109600@qq.com")
    @PostMapping(value = "/list")
    public List<Employee> list() {
        List<Employee> list = employeeService.findAll();
        log.info("list:{}", list);
        return list;
    }

    /** 通过id查询 */
    @ApiOperation("通过id查询")
    @ApiOperationSupport(author = "steve,email=1518109600@qq.com")
    @GetMapping(value = "/{id}")
    public Employee findById(@PathVariable @Min(1) Long id) {
        Employee employee = employeeService.findById(id);
        log.info("employee : {} ", employee);
        return employee;
    }

    /** 新增雇员 */
    @ApiOperation("新增雇员")
    @ApiOperationSupport(author = "steve,email=1518109600@qq.com")
    @PostMapping(value = "/save")
    public Employee save(@RequestBody @Validated Employee employee) {
        return employeeService.save(employee);
    }

    /** 更新雇员 */
    @ApiOperation("更新雇员")
    @ApiOperationSupport(author = "steve,email=1518109600@qq.com")
    @PostMapping(value = "update")
    public Employee update(@RequestBody @Validated Employee employee) {
        return employeeService.update(employee);
    }

    /** 测试返回值类型为字符串 */
    @ApiOperation("测试返回值类型为字符串")
    @ApiOperationSupport(author = "steve,email=1518109600@qq.com")
    @GetMapping(value = "/testString")
    public String testString() {
        return "test response body is String";
    }

    /** 获取环境配置 */
    @ApiOperation("获取环境配置")
    @ApiOperationSupport(author = "steve,email=1518109600@qq.com")
    @GetMapping(value = "getProfile")
    public String getProfile() {
        String[] defaultProfiles = environment.getActiveProfiles();
        return Arrays.toString(defaultProfiles);
    }

    /** 批量保存 */
    @ApiOperation("批量保存")
    @ApiOperationSupport(author = "steve,email=1518109600@qq.com")
    @PostMapping(value = "/saveList")
    public boolean saveList(@RequestBody @Validated List<Employee> employees) {
        log.info("employees : {}", employees);
        return false;
    }


}
